<?php /** 修改题库 api **/
require_once '../../config/req.conf.php';
require_once '../../utils/CommonMethods.php';
require_once '../../utils/VerifyFormData.php';
require_once '../../utils/DBUtils.php';

/* 1. token认证和角色权限校验 */
$headers = apache_request_headers(); // 请求头信息获取
$info = VerifyFormData::verifyRole($headers['Authorization'], "root");
if( CommonMethods::isEmpty($info) ){
    RespJson::fail("没有访问权限");
    return;
}

/* 2. 对表单数据进行校验 */
if( !VerifyFormData::addQuestionBankDataVerify($_POST) ) return;

/* 3. 数据库判断是否存在该题目信息 */
DBUtils::init();
$has = DBUtils::queryOne(DBUtils::getSql("php_questions")->where("id = {$_POST['id']}")->select());
if( CommonMethods::isEmpty($has) ){
    RespJson::fail("不存在该题目");
    return;
}

/* 4. 存在, 那么就对数据库进行修改 */
$sql = DBUtils::getSql("php_questions")
    ->set("question = '{$_POST['question']}', type = '{$_POST['type']}', optionA = '{$_POST['optionA']}', optionB = '{$_POST['optionB']}', optionC = '{$_POST['optionC']}', optionD = '{$_POST['optionD']}', answer = '{$_POST['answer']}'")
    ->where("id = {$_POST['id']}")
    ->update();
$success = DBUtils::execute($sql);
if( $success ){
    RespJson::success();
    return;
}else{
    RespJson::fail("修改出现错误, 请重试");
    return;
}